package com.espirit.eap.sa;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Transient;

import org.apache.commons.lang.StringUtils;
import org.hibernate.annotations.Index;
import org.hibernate.annotations.Parameter;
import org.hibernate.annotations.Table;
import org.hibernate.annotations.Type;
import org.hibernate.annotations.TypeDef;
import org.hibernate.annotations.TypeDefs;

import com.espirit.eap.ICheckModelDuplicateInfo;
import com.espirit.eap.annotation.ModelDisplayProperty;
import com.espirit.eap.manager.EnumUserType;
import com.googlecode.cswish.model.Saas;

@Entity
//@SecondaryTable(name="privilege")
@TypeDefs(@TypeDef(name="scopeType" ,typeClass=EnumUserType.class,
	parameters={@Parameter(name  = "enumClassName" , value  = "com.espirit.eap.sa.ScopeType")}))
@Table(indexes={@Index(name="saas_idx_privilege", columnNames={"modelSimpleName","button","scopeType"})}, appliesTo = "saas_privilege")
@ModelDisplayProperty("i18nName")
public class Privilege implements ICheckModelDuplicateInfo, Serializable {
	private static final long serialVersionUID = 2574324600742514145L;
	
	private Integer id;
	private String name;
	private String modelSimpleName;
	private String mappingModelSimpleName;
	private String method;
//	private List<FieldPermission> fieldPermissions;
	private ScopeType scopeType;
	private String i18n;
	private String condition;
	
//	private Integer sid;
//	public Integer getSid() {
//		return sid;
//	}
//
//	public void setSid(Integer sid) {
//		this.sid = sid;
//	}
	
	@Id
	@GeneratedValue
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	public Privilege() {
		super();
	}
	public Privilege(String modelSimpleName, String method) {
		super();
		this.modelSimpleName = modelSimpleName;
		this.method = method;
	}
	public String getModelSimpleName() {
		return modelSimpleName;
	}
	public void setModelSimpleName(String modelSimpleName) {
		this.modelSimpleName = modelSimpleName;
	}
	@Column(name="button")
	public String getMethod() {
		return method;
	}
	public void setMethod(String method) {
		this.method = method;
	}
//	@OneToMany(targetEntity=FieldPermission.class,cascade = CascadeType.ALL,mappedBy="parent",fetch=FetchType.LAZY)
//	public List<FieldPermission> getFieldPermissions() {
//		return fieldPermissions;
//	}
//	public void setFieldPermissions(List<FieldPermission> fieldPermissions) {
//		this.fieldPermissions = fieldPermissions;
//	}
//	public List<FieldPermission> safeFieldPermissions() {
//		if (fieldPermissions == null){
//			fieldPermissions = new ArrayList<FieldPermission>();
//		}
//		return fieldPermissions;
//	}
	
	@Type(type="scopeType")
	public ScopeType getScopeType() {
		return scopeType;
	}
	public void setScopeType(ScopeType scopeType) {
		this.scopeType = scopeType;
	}
	
	
	public void setI18n(String i18n) {
		this.i18n = i18n;
	}

	public String getI18n() {
		return i18n;
	}
	
	@Transient
	public String getProductName() {
		int index = modelSimpleName.indexOf('.');
		if (index >= 0) {
			return modelSimpleName.substring(0, index);
		} else {
			return modelSimpleName;
		}
	}

	public void setCondition(String condition) {
		this.condition = condition;
	}
	
	@Column(name="privilegeCondition")
	public String getCondition() {
		return condition;
	}
	private final static String[] gatherProperties = new String[]{"name"};
	@Override
	public String[] gatherProperties() {
		return gatherProperties;
	}
	public String getMappingModelSimpleName() {
		return mappingModelSimpleName;
	}
	public void setMappingModelSimpleName(String mappingModelSimpleName) {
		this.mappingModelSimpleName = mappingModelSimpleName;
	}
	
	@Transient
	public String getCategory() {
		String name = mappingModelSimpleName;
		if (mappingModelSimpleName == null || mappingModelSimpleName.length() == 0) {
			name = modelSimpleName;
		}
		if (StringUtils.isEmpty(name)) return null;
		int index = name.indexOf('.');
		if (index > 0) {
			// skip the first site name
			int nextIndex = name.indexOf('.', index + 1);
			if (nextIndex > 0) {
				return name.substring(index + 1, nextIndex);
			} else {
				return name.substring(0, index);
			}
		} else {
			return "others";
		}
	}
	
}